/**
 * 初始化员工积分详情对话框
 */
var EmpPointsInfoDlg = {
    empPointsInfoData : {}
};

/**
 * 清除数据
 */
EmpPointsInfoDlg.clearData = function() {
    this.empPointsInfoData = {};
};

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
EmpPointsInfoDlg.set = function(key, val) {
    this.empPointsInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
};

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
EmpPointsInfoDlg.get = function(key) {
    return $("#" + key).val();
};

/**
 * 关闭此对话框
 */
EmpPointsInfoDlg.close = function() {
    parent.layer.close(window.parent.EmpPoints.layerIndex);
};

/**
 * 收集数据
 */
EmpPointsInfoDlg.collectData = function() {
    this
    .set('id')
    .set('empCode')
    .set('empName')
    .set('year')
    .set('modularPonit')
    ;
};

/**
 * 提交添加
 */
EmpPointsInfoDlg.addSubmit = function() {

    $('.form-horizontal').data('bootstrapValidator').validate();
    if(!$('.form-horizontal').data('bootstrapValidator').isValid()){
        return ;
    }

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/empPoints/add", function(data){
        Feng.success("添加成功!");
        window.parent.EmpPoints.table.refresh();
        EmpPointsInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.empPointsInfoData);
    ajax.start();
};

/**
 * 提交修改
 */
EmpPointsInfoDlg.editSubmit = function() {

    $('.form-horizontal').data('bootstrapValidator').validate();
    if(!$('.form-horizontal').data('bootstrapValidator').isValid()){
        return ;
    }

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/empPoints/update", function(data){
        Feng.success("修改成功!");
        window.parent.EmpPoints.table.refresh();
        EmpPointsInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.empPointsInfoData);
    ajax.start();
};

/**
 * 规则计算
 */
EmpPointsInfoDlg.ruleCal = function () {
    $("#annualPeriod").blur(function(){
        var annualPeriod = $("#annualPeriod").val();
        $.ajax({
            type: "post",
            url: Feng.ctxPath + "/EmpPoints/integralCal",
            dataType: "json",
            data: "hours="+annualPeriod,
            success: function(data) {
                debugger
                // if(data.data){
                    $("#annualCredits").val(data.data);
                // }
            }
        });

    });
};

/**
 * 上传提交
 */
EmpPointsInfoDlg.upload = function () {
    layui.use('upload', function(){
        var upload = layui.upload;
        //固定格式导入
        upload.render({
            elem: '#selectFile'
            ,url: '/EmpPoints/import'
            ,auto: false
            ,accept: "file"
            ,exts: "xls|xlsx"
            ,bindAction: '#fixImport'
            ,before: function () {
                layer.msg("正在读取文件，请稍后...")
                loadIndex = layer.load(2);
            }
            ,done: function(res){
                layer.close(loadIndex);
                if(res.ok){
                    layer.msg(res.ok);
                }else{
                    layer.msg(res.fail);
                }

            }
        });

    });
};

/**
 * 提交校验
 */
EmpPointsInfoDlg.formValidator = function () {

    $('.form-horizontal').bootstrapValidator({
        message: '校验失败',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            name: {
                message: '用户名验证失败',
                validators: {
                    notEmpty: {
                        message: '员工姓名不能为空'
                    }
                }
            }
        }
    });
};

/**
 * 选择员工
 */
EmpPointsInfoDlg.selectEmp = function () {
    layer.open({
        type: 2,
        area: ['800px', '500px'],
        fixed: false, //不固定
        maxmin: true,
        content: '../employee/select_list',
        btn: ['确定'],
        yes: function(index, layero){
            var res = window["layui-layer-iframe" + index].Employee.callbackdata();
            $("#empName").val(res.empName);
            $("#empCode").val(res.empCode);
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        }
    });
};


$(function() {
    EmpPointsInfoDlg.formValidator();
    EmpPointsInfoDlg.upload();
});
